﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Text.RegularExpressions;

namespace ueEngine.Web.WebLogic
{
    public static class StaticURL
    {
        /// <summary>
        /// 读取参数
        /// </summary>
        /// <param name="context"></param>
        /// <param name="paramName">如果读取参数id，则必须是第一个参数</param>
        /// <returns></returns>
        public static string GetParameter(this HttpContext context, string paramName)
        {
            if (String.IsNullOrEmpty(paramName))
            {
                return "";
            }
            if (context.Request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                if (!String.IsNullOrEmpty(context.Request.Form[paramName]))
                {
                    return context.Request.Form[paramName];
                }
            }
            else
            {
                string url = context.Request.RawUrl;
                if (url.IndexOf('?') >= 0)
                {
                    return context.Request.QueryString[paramName];
                }
                else
                {
                    if (paramName.Equals("id", StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (url.IndexOf('_') >= 0)
                        {
                            string id = url.Substring(url.IndexOf('_') + 1);
                            if (id.IndexOf('-') >= 0)
                            {
                                //排除后面的-字符
                                return id.Substring(0, id.IndexOf('-'));
                            }
                            return id;
                        }
                        else
                        {
                            string laststr = url.Substring(url.LastIndexOf('/') + 1);
                            var reg = new Regex("^\\d+");
                            var match = reg.Match(laststr);
                            if (match.Success)
                            {
                                return match.Value;
                            }
                        }
                    }
                    else if (url.IndexOf(paramName + "=", StringComparison.OrdinalIgnoreCase) > -1)
                    {
                        string parameter = url.Substring(url.IndexOf(paramName + "=", StringComparison.OrdinalIgnoreCase) + paramName.Length + 1);
                        if (parameter.IndexOf('-') > 0)
                        {
                            parameter = parameter.Substring(0, parameter.IndexOf('-'));
                        }
                        return parameter;
                    }
                }
            }
            return "";
        }
        /// <summary>
        /// 读取参数
        /// </summary>
        /// <param name="context"></param>
        /// <param name="paramName"></param>
        /// <returns></returns>
        public static string GetParameter(this HttpContextBase context, string paramName)
        {
            if (String.IsNullOrEmpty(paramName))
            {
                return "";
            }
            if (context.Request.HttpMethod.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                if (!String.IsNullOrEmpty(context.Request.Form[paramName]))
                {
                    return context.Request.Form[paramName];
                }
            }
            else
            {
                string url = context.Request.RawUrl;
                if (url.IndexOf('?') >= 0)
                {
                    return context.Request.QueryString[paramName];
                }
                else
                {
                    if (paramName.Equals("id", StringComparison.CurrentCultureIgnoreCase))
                    {
                        if (url.IndexOf('_') >= 0)
                        {
                            string id = url.Substring(url.IndexOf('_') + 1);
                            if (id.IndexOf('-') >= 0)
                            {
                                //排除后面的-字符
                                return id.Substring(0, id.IndexOf('-'));
                            }
                            return id;
                        }
                        else
                        {
                            string laststr = url.Substring(url.LastIndexOf('/') + 1);
                            var reg = new Regex("^\\d+");
                            var match = reg.Match(laststr);
                            if (match.Success)
                            {
                                return match.Value;
                            }
                        }
                    }
                    else if (url.IndexOf(paramName + "=", StringComparison.OrdinalIgnoreCase) > -1)
                    {
                        string parameter = url.Substring(url.IndexOf(paramName + "=", StringComparison.OrdinalIgnoreCase) + paramName.Length + 1);
                        if (parameter.IndexOf('-') > 0)
                        {
                            parameter = parameter.Substring(0, parameter.IndexOf('-'));
                        }
                        return parameter;
                    }
                }
            }
            return "";
        }
    }
}
